package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;


    @RequiredPermission(name="投票页面",expression="activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "activity/list";
    }


    @RequiredPermission(name="投票删除",expression="activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票新增/编辑",expression="activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name="投票新增/编辑跳转",expression="activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("activity",activityService.get(id));
        }
        return "activity/input";
    }

    @RequiredPermission(name="更新投票状态",expression="activity:updateStatus")
    @RequestMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id) {
        activityService.updateStatus(id);
        return new JsonResult();
    }

    @RequiredPermission(name="增加投票活动",expression="activity:saveActivity")
    @RequestMapping("/saveActivity")
    @ResponseBody
    public JsonResult saveActivity(Activity activity, String[] itemArr) {
        activityService.saveActivity(activity, itemArr);
        return new JsonResult();
    }

    @RequiredPermission(name="更新投票情况",expression="activity:increaseNum")
    @RequestMapping("/increaseNum")
    @ResponseBody
    public JsonResult increaseNum(Long activityId, Long[] itemIds) {
        activityService.increaseNum(activityId, itemIds);
        return new JsonResult();
    }

    @RequestMapping("/checkin")
    @ResponseBody
    public JsonResult checkin(Long id) {
        activityService.checkin(id);
        return new JsonResult();
    }

    @RequiredPermission(name="投票情况显示",expression="activity:show")
    @RequestMapping("/show")
    public String show(Long id, Model model) {
        if(id != null){
            model.addAttribute("activity", activityService.get(id));
        }
        return "activity/show";
    }

    @RequiredPermission(name="查看",expression="activity:seeActivity")
    @RequestMapping("/seeActivity")
    @ResponseBody
    public JsonResult seeActivity(Long id) {
        activityService.seeActivity(id);
        return new JsonResult();
    }
}
